#pragma once
#include<iostream>
#include<sys/epoll.h>
#include"Socket.hpp"
#include"Connection.hpp"
#include"Reactor.hpp"
class Listener
{
public:
    Listener(uint16_t port):_port(port),_listensock(make_unique<TcpSocket>())
    {
        _listensock->BuildListenSocket(port);
    }
    void Accepter(Connection* con)
    {
        while(true)
        {
            InetAddr addr;
            int code = 0;
            int sockfd =_listensock->Accepter(&addr,code);
            if(sockfd > 0)
            {
                con->_R->AddConnection(sockfd,EPOLLIN|EPOLLET,NormalConnection);
            }
            else
            {
                if(code == EWOULDBLOCK)
                {
                    break;
                }
                else if(code == EINTR)
                {
                    continue;
                }
                else
                {
                    break;//链接获取失败
                }

            }
        }
    }
    int Sockfd()
    {return _listensock->Sockfd();}
private:
    uint16_t _port;
    unique_ptr<Socket> _listensock;
};